/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientGui.java
 *
 * Created on 22.8.2009, 12:35:42
 */

package YottaTron;

import YottaTron.communication.CommunicationUDP;
import java.awt.event.ActionEvent;

/**
 *
 * @author Lou
 */
public class ChatGui extends javax.swing.JPanel {

    CommunicationUDP comm;

    /** Creates new form ClientGui */
    public ChatGui() {
        initComponents();
    }


    public void addComm(CommunicationUDP comm){
        this.comm = comm;
    }

    public void actionPerformed(ActionEvent e)
   /* Either a message is to be sent or the "Who"
      button has been pressed. */
   {
//     if (e.getSource() == taChatInput){
//         comm.sendMessage(taChatInput.getText().trim());
//     }

   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        taChat = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        taChatInput = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        tfHost = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfNick = new javax.swing.JTextField();

        taChat.setColumns(20);
        taChat.setEditable(false);
        taChat.setRows(5);
        jScrollPane1.setViewportView(taChat);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(50, 85));

        taChatInput.setColumns(20);
        taChatInput.setRows(5);
        taChatInput.setMargin(new java.awt.Insets(0, 0, 0, 0));
        taChatInput.setMaximumSize(new java.awt.Dimension(21, 21));
        taChatInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                taChatInputKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taChatInputKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                taChatInputKeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(taChatInput);

        jLabel1.setText("server IP:");

        tfHost.setText("localhost");
        tfHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfHostActionPerformed(evt);
            }
        });
        tfHost.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfHostFocusLost(evt);
            }
        });

        jLabel2.setText("nick:");

        tfNick.setText("abcdefghijkl");
        tfNick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfNickActionPerformed(evt);
            }
        });
        tfNick.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfNickFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfNick, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfNick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(tfHost, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void taChatInputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taChatInputKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER){
            comm.sendMessage(taChatInput.getText().trim());
        }
    }//GEN-LAST:event_taChatInputKeyPressed

    private void taChatInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taChatInputKeyTyped

    }//GEN-LAST:event_taChatInputKeyTyped

    private void taChatInputKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taChatInputKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER){
            taChatInput.setText("");
        }
    }//GEN-LAST:event_taChatInputKeyReleased

    private void tfHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfHostActionPerformed
        //comm.connectToServer(tfHost.getText(), comm.serverPort);
}//GEN-LAST:event_tfHostActionPerformed

    private void tfNickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfNickActionPerformed
        if (comm.clientID > 0) comm.sendNick(tfNick.getText());
        System.out.println("nick changed");
    }//GEN-LAST:event_tfNickActionPerformed

    private void tfNickFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfNickFocusLost
        if (comm.clientID > 0) comm.sendNick(tfNick.getText());
        System.out.println("nick changed");
    }//GEN-LAST:event_tfNickFocusLost

    private void tfHostFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfHostFocusLost
        //comm.connectToServer(tfHost.getText(), comm.serverPort);
    }//GEN-LAST:event_tfHostFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    public javax.swing.JTextArea taChat;
    public javax.swing.JTextArea taChatInput;
    public javax.swing.JTextField tfHost;
    public javax.swing.JTextField tfNick;
    // End of variables declaration//GEN-END:variables

}
